package com.yuanfeng.job.controller;

import com.alibaba.fastjson.JSON;
import com.yuanfeng.commoms.config.YFConfig;
import com.yuanfeng.commoms.dto.userms.UserInfoDTO;
import com.yuanfeng.commoms.exception.BizCodeEnume;
import com.yuanfeng.commoms.exception.DefinedException;
import com.yuanfeng.commoms.exception.GlobalExceptionEnum;
import com.yuanfeng.commoms.util.RedisUtil;
import com.yuanfeng.commoms.util.ResponseResult;
import com.yuanfeng.job.service.JobPlatformService;
import org.springframework.beans.factory.annotation.Autowired;

/**
 * @ClassName BaseController
 * @Description 基础controller
 * @Author hyq
 * @Date 2023/5/18 17:39
 * @Version 1.0
 **/

public class BaseController {
    @Autowired
    protected JobPlatformService jobPlatformService;
    /**
     * 根据token得到userInfo
     *
     * @param token
     * @return
     */
    public ResponseResult getUserInfoByToken(String token) {
        //从redis得到token转换userInfo
        boolean isExist = RedisUtil.exists(YFConfig.YF_USER_TOKEN_USER + token);
        if (!isExist) {
            throw new DefinedException(GlobalExceptionEnum.NO_FORBIDDEN.getErrorCode(), BizCodeEnume.TOKEN_IS_INVALID.getMsg());
        } else {
            String userInfoStr = RedisUtil.get(YFConfig.YF_USER_TOKEN_USER + token);
            UserInfoDTO dto = JSON.parseObject(userInfoStr, UserInfoDTO.class);
            if (dto != null) {
                return ResponseResult.success(dto.getUser());
            }
            return ResponseResult.fail(BizCodeEnume.TOKEN_IS_ILLEGAL.getCode(), BizCodeEnume.TOKEN_IS_ILLEGAL.getMsg());

        }

    }
}
